import xml.etree.ElementTree as ET
import enum
from .PatternConvertor import *
class NodeType(enum.Enum):
    Nor = 1
    Rul = 2



class XmlBuilder(object):
    def __init__(self):
        pass



    NormAttrs = {'Index':'0',
                 'Version':'3',
                 'DevType':'508',
                 'Standard':'smc_tel',
                 'MappingFile':'bluedonMapping.xml',}
    RuleAttrs = {
        'Name':'auto_generated',
        'Pattern':'',
        'Action':'',
    }

    @staticmethod
    def setPara(ParaDic, type):
        """
        set attribute of tags
        :param paraDic:
        :return:
        """
        if type == NodeType.Nor:
            for (k,v) in ParaDic.items():
                if XmlBuilder.NormAttrs.get(k) is not None:
                    XmlBuilder.NormAttrs[k] = v
        elif type == NodeType.Rul:
            for (k,v) in ParaDic.items():
                if XmlBuilder.RuleAttrs.get(k) is not None:
                    XmlBuilder.RuleAttrs[k] = v


    def buildStructure(self):
        '''
        build xml file
        :return:
        '''
        root = ET.Element('root')
        norm = ET.SubElement(root,'Normalize')
        rule1 = ET.SubElement(norm,'Rule')
        self.__setNodeAttr(rule1, NodeType.Rul)
        self.__setNodeAttr(norm, NodeType.Nor)

        ET.dump(root)
        tree = ET.ElementTree(root)
        tree.write('output.xml',encoding='GB2312')

    def __setNodeAttr(self, node, attrType):
        if attrType == NodeType.Nor:
            for (k,v) in XmlBuilder.NormAttrs.items():
                node.set(k,v)
        elif attrType == NodeType.Rul:
            for (k,v) in XmlBuilder.RuleAttrs.items():
                node.set(k,v)



